home *** CD-ROM | disk | FTP | other *** search
/ Windows 95 API Bible / Windows 95 API Bible 3 Disc Set.iso / Win32 API Bible Book 1 of 3.iso / chapte12 / offvorgx.c < prev    next >
C/C++ Source or Header  |  1996-01-30  |  6KB  |  206 lines

  1.  
  2. #include <windows.h>  
  3. #include "OffVOrgX.h" 
  4.  
  5.  
  6. #if defined (WIN32)
  7.     #define IS_WIN32 TRUE
  8. #else
  9.     #define IS_WIN32 FALSE
  10. #endif
  11.  
  12. #define IS_NT      IS_WIN32 && (BOOL)(GetVersion() < 0x80000000)
  13. #define IS_WIN32S  IS_WIN32 && (BOOL)(!(IS_NT) && (LOBYTE(LOWORD(GetVersion()))<4))
  14. #define IS_WIN95   (BOOL)(!(IS_NT) && !(IS_WIN32S)) && IS_WIN32
  15.  
  16. HINSTANCE hInst;   // current instance
  17.  
  18. LPCTSTR lpszAppName  = "MyApp";
  19. LPCTSTR lpszTitle    = "My Application"; 
  20.  
  21. BOOL RegisterWin95( CONST WNDCLASS* lpwc );
  22.  
  23. int APIENTRY WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance,
  24.                       LPTSTR lpCmdLine, int nCmdShow)
  25. {
  26.    MSG      msg;
  27.    HWND     hWnd; 
  28.    WNDCLASS wc;
  29.  
  30.    // Register the main application window class.
  31.    //............................................
  32.    wc.style         = CS_HREDRAW | CS_VREDRAW | CS_OWNDC;
  33.    wc.lpfnWndProc   = (WNDPROC)WndProc;       
  34.    wc.cbClsExtra    = 0;                      
  35.    wc.cbWndExtra    = 0;                      
  36.    wc.hInstance     = hInstance;              
  37.    wc.hIcon         = LoadIcon( hInstance, lpszAppName ); 
  38.    wc.hCursor       = LoadCursor(NULL, IDC_ARROW);
  39.    wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
  40.    wc.lpszMenuName  = lpszAppName;              
  41.    wc.lpszClassName = lpszAppName;              
  42.  
  43.    if ( IS_WIN95 )
  44.    {
  45.       if ( !RegisterWin95( &wc ) )
  46.          return( FALSE );
  47.    }
  48.    else if ( !RegisterClass( &wc ) )
  49.       return( FALSE );
  50.  
  51.    hInst = hInstance; 
  52.  
  53.    // Create the main application window.
  54.    //....................................
  55.    hWnd = CreateWindow( lpszAppName, 
  56.                         lpszTitle,    
  57.                         WS_OVERLAPPEDWINDOW, 
  58.                         CW_USEDEFAULT, 0, 
  59.                         CW_USEDEFAULT, 0,  
  60.                         NULL,              
  61.                         NULL,              
  62.                         hInstance,         
  63.                         NULL               
  64.                       );
  65.  
  66.    if ( !hWnd ) 
  67.       return( FALSE );
  68.  
  69.    ShowWindow( hWnd, nCmdShow ); 
  70.    UpdateWindow( hWnd );         
  71.  
  72.    while( GetMessage( &msg, NULL, 0, 0) )   
  73.    {
  74.       TranslateMessage( &msg ); 
  75.       DispatchMessage( &msg );  
  76.    }
  77.  
  78.    return( msg.wParam ); 
  79. }
  80.  
  81.  
  82. BOOL RegisterWin95( CONST WNDCLASS* lpwc )
  83. {
  84.    WNDCLASSEX wcex;
  85.  
  86.    wcex.style         = lpwc->style;
  87.    wcex.lpfnWndProc   = lpwc->lpfnWndProc;
  88.    wcex.cbClsExtra    = lpwc->cbClsExtra;
  89.    wcex.cbWndExtra    = lpwc->cbWndExtra;
  90.    wcex.hInstance     = lpwc->hInstance;
  91.    wcex.hIcon         = lpwc->hIcon;
  92.    wcex.hCursor       = lpwc->hCursor;
  93.    wcex.hbrBackground = lpwc->hbrBackground;
  94.    wcex.lpszMenuName  = lpwc->lpszMenuName;
  95.    wcex.lpszClassName = lpwc->lpszClassName;
  96.  
  97.    // Added elements for Windows 95.
  98.    //...............................
  99.    wcex.cbSize = sizeof(WNDCLASSEX);
  100.    wcex.hIconSm = LoadImage(wcex.hInstance, lpwc->lpszClassName, 
  101.                             IMAGE_ICON, 16, 16,
  102.                             LR_DEFAULTCOLOR );
  103.             
  104.    return RegisterClassEx( &wcex );
  105. }
  106.  
  107. LRESULT CALLBACK WndProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
  108. {
  109.    switch( uMsg )
  110.    {
  111.       case WM_SIZE  : 
  112.               {
  113.                  HDC hDC = GetDC( hWnd );
  114.  
  115.                  // Set the mapping mode and set the
  116.                  // scaling of the DC.
  117.                  //.................................
  118.                  SetMapMode( hDC, MM_ISOTROPIC );
  119.                  SetWindowExtEx( hDC, 1, 1, NULL );
  120.                  SetViewportExtEx( hDC, 2, -2, NULL );
  121.  
  122.                  // Set the origin of the device context.
  123.                  //......................................
  124.                  SetWindowOrgEx( hDC, -10, -10, NULL );
  125.                  SetViewportOrgEx( hDC, 0, HIWORD(lParam), NULL );
  126.               }
  127.               break;
  128.  
  129.       case WM_COMMAND :
  130.               switch( LOWORD( wParam ) )
  131.               {
  132.                  case IDM_TEST :
  133.                         {
  134.                            HDC   hDC;
  135.                            POINT ptWOrg;
  136.                            POINT ptVOrg;
  137.                            char  szMsg[40];
  138.  
  139.                            hDC = GetDC( hWnd );
  140.  
  141.                            // Change the origin by an offset.
  142.                            //................................
  143.                            OffsetWindowOrgEx( hDC, 10, 0, NULL );
  144.                            OffsetViewportOrgEx( hDC, 5, 5, NULL );               
  145.  
  146.                            // Display the origin of the 
  147.                            // device context.
  148.                            //..........................
  149.                            GetWindowOrgEx( hDC, &ptWOrg );
  150.                            GetViewportOrgEx( hDC, &ptVOrg );
  151.  
  152.                            wsprintf( szMsg, "Viewport Org = %d, %d", 
  153.                                             ptVOrg.x, ptVOrg.y );
  154.                            TextOut( hDC, 0, 0, szMsg, strlen( szMsg ) );
  155.  
  156.                            wsprintf( szMsg, "Window Org = %d, %d", 
  157.                                             ptWOrg.x, ptWOrg.y );
  158.                            TextOut( hDC, 0, 10, szMsg, strlen( szMsg ) );
  159.                         }
  160.                         break;
  161.  
  162.                  case IDM_ABOUT :
  163.                         DialogBox( hInst, "AboutBox", hWnd, (DLGPROC)About );
  164.                         break;
  165.  
  166.                  case IDM_EXIT :
  167.                         DestroyWindow( hWnd );
  168.                         break;
  169.               }
  170.               break;
  171.       
  172.       case WM_DESTROY :
  173.               PostQuitMessage(0);
  174.               break;
  175.  
  176.       default :
  177.             return( DefWindowProc( hWnd, uMsg, wParam, lParam ) );
  178.    }
  179.  
  180.    return( 0L );
  181. }
  182.  
  183.  
  184. LRESULT CALLBACK About( HWND hDlg,           
  185.                         UINT message,        
  186.                         WPARAM wParam,       
  187.                         LPARAM lParam)
  188. {
  189.    switch (message) 
  190.    {
  191.        case WM_INITDIALOG: 
  192.                return (TRUE);
  193.  
  194.        case WM_COMMAND:                              
  195.                if (   LOWORD(wParam) == IDOK         
  196.                    || LOWORD(wParam) == IDCANCEL)    
  197.                {
  198.                        EndDialog(hDlg, TRUE);        
  199.                        return (TRUE);
  200.                }
  201.                break;
  202.    }
  203.  
  204.    return (FALSE); 
  205. }
  206.